import { _decorator, Component, director, Label, Node } from 'cc';
import { EventDispatcher } from './EventDispatcher';
import { GameState } from './GameState';
import { mainScene } from './mainScene';
const { ccclass, property } = _decorator;

@ccclass('Revive')
export class Revive extends Component {
    @property(Label)
    freeTimes:Label = null;
    start() {
        this.node.active = false;
        EventDispatcher.getInstTarget().on(EventDispatcher.GAME_OVER, this.show, this);
    }
    protected onDestroy(): void {
        EventDispatcher.getInstTarget().off(EventDispatcher.GAME_OVER, this.show, this);
    }

    show() {
        this.freeTimes.string = `(${GameState.reviveTimes}/1)`
        this.node.active = true;
    }

    clickRevive() {
        if(GameState.reviveTimes > 0){
            GameState.reviveTimes -= 1;
            this.node.active = false;
            EventDispatcher.getInstTarget().emit(EventDispatcher.REVIVE_GAME);
        }else{
            EventDispatcher.getInstTarget().emit(EventDispatcher.SHOW_TIPS, '无复活次数')
        }
    }

    clickThanks() {
        this.node.active = false;
        this.node.parent.getComponent(mainScene).onBackClick();
    }
}


